package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.service.impl.LoanContractAuditService;
import com.car.loan.project.xr.util.bo.business.ContractAuditBo;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.ContractAuditStatus;
import com.car.loan.project.xr.util.enums.InsuranceInfoStatus;
import com.car.loan.project.xr.util.enums.MakeLoansInfoStatus;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.CarInfoInputVo;
import com.car.loan.project.xr.util.vo.LoanContractAuditVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/12/8 10:33
 */
@RestController
@RequestMapping("xr/contract/audit")
@Api(value = "合同审核", tags = "车贷业务-合同审核")
public class ContractAuditController {

    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanStatusService loanStatusService;

    @Autowired
    private LoanContractAuditService loanContractAuditService;

    @PreAuthorize("@ss.hasPermi('contract:audit:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取合同审核列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.CONTRACT_AUDIT_STATUS)
    public AjaxResult applyList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }


    @PreAuthorize("@ss.hasPermi('contract:audit:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "获取合同审核详情", response = LoanContractAuditVo.class)
    public AjaxResult saveApply(@PathVariable Long loanId) {
        return AjaxResult.success(loanService.getContractAuditDetail(loanId));
    }


    @PreAuthorize("@ss.hasPermi('contract:audit:audit')")
    @PostMapping("audit")
    @Log(title = "合同审核", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "合同审核")
    public AjaxResult audit(@RequestBody @Valid ContractAuditBo contractAuditBo) {
        return loanContractAuditService.audit(contractAuditBo);
    }

    @PreAuthorize("@ss.hasPermi('contract:audit:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回合同审核", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回合同审核", notes = "放贷信息录入后不可撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        Long makeLoansInfoStatus = loanStatus.getMakeLoansInfoStatus();
        if (MakeLoansInfoStatus.SUBMIT.status == makeLoansInfoStatus) {
            return AjaxResult.error("放贷信息已录入，不可撤回");
        }
        loanStatus.setContractAuditStatus((long) ContractAuditStatus.INIT.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }
}
